package com.polaris.live.adapter.backpack

import android.view.View
import com.blankj.utilcode.util.StringUtils
import com.bumptech.glide.Glide
import com.polaris.live.R
import com.polaris.live.R.drawable
import com.polaris.live.common.adapter.BaseBindingAdapter
import com.polaris.live.common.adapter.VBViewHolder
import com.polaris.live.common.cacha.CacheUtil
import com.polaris.live.databinding.ItemBackpackToolBinding
import com.polaris.live.resp.back_resp.UserBackpackConsumableResp
import com.polaris.live.translation.R.string
import com.polaris.live.utils.image.ImageUtils
import java.util.concurrent.TimeUnit

/**
 * BackPackToolAdapter  道具adapter
 *
 * @author Created by 半仙 on 2024/1/31/031 11:44
 */
class BackPackToolAdapter : BaseBindingAdapter<ItemBackpackToolBinding, UserBackpackConsumableResp>() {

    private var mSelectIndex = -1

    init {
        addChildClickViewIds(R.id.tv_chick1)
    }

    override fun convert(
        holder: VBViewHolder<ItemBackpackToolBinding>,
        item: UserBackpackConsumableResp,
    ) {
        holder.vb.apply {
            val position = holder.absoluteAdapterPosition
            if (position == mSelectIndex) {
                consTitle.setBackgroundResource(drawable.back_pack_select_back)
            } else {
                consTitle.setBackgroundResource(drawable.r8_back_ffffff)
            }
            tvName.setText(item.name, true)
            Glide.with(icImage).load(ImageUtils.imgPath(item.displayUrl)).into(icImage)
            llTag.visibility = View.VISIBLE
            setTime(holder, item)
            if (item.amount > 1) {
                tvCount.visibility = View.VISIBLE
                tvCount.text = "x${item.amount}"
            } else {
                tvCount.visibility = View.GONE
            }

        }
    }

    override fun convert(
        holder: VBViewHolder<ItemBackpackToolBinding>,
        item: UserBackpackConsumableResp, data: List<Any>,
    ) {
        when (data.getOrNull(0)) {
            "notify" -> setTime(holder, item)
            "show" -> show(holder)
            "hide" -> hide(holder)
        }
    }

    fun show(holder: VBViewHolder<ItemBackpackToolBinding>) {
        holder.vb.tvName.startAndResume()
    }

    fun hide(holder: VBViewHolder<ItemBackpackToolBinding>) {
        holder.vb.tvName.pause()
    }

    private fun setTime(holder: VBViewHolder<ItemBackpackToolBinding>, item: UserBackpackConsumableResp) {
        holder.vb.apply {
            val expiredTime = item.expiredTime
            if (expiredTime.isNullOrEmpty()) {
                ivTimeIcon.visibility = View.GONE
                tvTime.text = StringUtils.getString(string.permanent)
            } else {
                ivTimeIcon.visibility = View.VISIBLE
                item.expireDays?.let {
                    if (it == 0L) {
                        tvTime.text = formatTime(expiredTime.toLong() - CacheUtil.getServerTime())
                    } else {
                        val (day, num) = if (it < 365) {
                            "day" to it
                        } else {
                            "year" to it / 365
                        }
                        tvTime.text = "$num$day"
                    }
                }
            }
        }

    }

    private fun formatTime(timeInMillis: Long): String {
        val days = TimeUnit.MILLISECONDS.toDays(timeInMillis)
        val hours = TimeUnit.MILLISECONDS.toHours(timeInMillis)
        val minutes = TimeUnit.MILLISECONDS.toMinutes(timeInMillis)
        val seconds = TimeUnit.MILLISECONDS.toSeconds(timeInMillis)
        return when {
            days > 0 -> StringUtils.getString(string.days, days)
            hours > 0 -> StringUtils.getString(string.hours, hours)
            minutes > 0 -> StringUtils.getString(string.minutes, minutes)
            else -> StringUtils.getString(string.seconds, seconds)
        }
    }

    fun setSelect(position: Int) {
        notifyItemChanged(mSelectIndex)

        mSelectIndex = position

        notifyItemChanged(mSelectIndex)
    }

    fun getLastSelect() = mSelectIndex

    companion object {

        const val NOT_HAVE = -1L             //装扮id等于-1是未获取
    }
}